﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MishraReader.Entities.ShareServices;
using MishraReader.ViewModels.Base;
using MetroLog;

namespace MishraReader.ViewModels.Settings.Options
{
    public class GenericConfigurationViewModel : DialogViewModelBase, IShareServiceConfigurationViewModel
    {
        private static readonly ILogger Log = LogManagerFactory.DefaultLogManager.GetLogger<GenericConfigurationViewModel>();
        private bool _hasError;
        private string _password;
        private string _username;

        public GenericConfigurationViewModel()
        {
        }

        public bool HasError
        {
            get { return _hasError; }
            set
            {
                Set(ref _hasError, value);
            }
        }

        public string Password
        {
            get { return _password; }
            set
            {
                Set(ref _password, value);
            }
        }

        public IServiceAccountViewModel ServiceAccountViewModel { get; set; }

        public string ServiceName
        {
            get { return ServiceAccountViewModel.ServiceName; }
        }

        public string Username
        {
            get { return _username; }
            set
            {
                Set(ref _username, value);
            }
        }

        public void Configure()
        {
            Username = ServiceAccountViewModel.ServiceUserName;
            Password = ServiceAccountViewModel.ServicePassword;

            ShowData = true;
        }

        public void Disconnect()
        {
        }

        protected override bool CanExecuteOkay()
        {
            if (!string.IsNullOrEmpty(Username) &&
                !string.IsNullOrEmpty(Password))
            {
                return true;
            }

            return false;
        }


        protected override void OnOkayExecuted()
        {
            if (CanExecuteOkay())
            {
                if (ServiceAccountViewModel.IsWellConfigured(Username, Password))
                {
                    HasError = false;
                    ShowData = false;
                    ServiceAccountViewModel.IsActivated = true;
                    ServiceAccountViewModel.ServiceUserName = Username;
                    ServiceAccountViewModel.ServicePassword = Password;
                }
                else
                {
                    HasError = true;
                }
            }
        }
    }
}